package com.gzsxy.sso.core.controller;

import com.gzsxy.sso.common.dto.request.MenuRequest;
import com.gzsxy.sso.common.dto.response.MenuRsp;
import com.gzsxy.sso.common.result.R;
import com.gzsxy.sso.core.entity.Function;
import com.gzsxy.sso.core.entity.Menu;
import com.gzsxy.sso.core.entity.vo.MenuQueryVo;
import com.gzsxy.sso.core.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 菜单管理
 * @date 2021/11/18 11:17
 */
@Api(description = "菜单管理")
@CrossOrigin  //解决跨域
@RestController
@RequestMapping("/admin/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("获取当前登录用户的菜单权限")
    @GetMapping("getUserMenu")
    public R getUserMenu(MenuQueryVo menuQueryVo){
        List<MenuRsp> menuRspList =  menuService.getUserMenu(menuQueryVo);
        return R.ok().data("data",menuRspList);
    }


    @ApiOperation("获取全部菜单功能")
    @GetMapping("getMenu")
    public R getMenu(MenuQueryVo menuQueryVo){
        List<MenuRsp> menuRspList =  menuService.getMenu(menuQueryVo);
        return R.ok().data("data",menuRspList);
    }


    /**
     * 该接口不对外使用
     */
    @ApiOperation("根据角色id查询所有功能列表")
    @GetMapping("/getfunctionByRoles")
    public R getfunctionByRoles(@ApiParam(value = "角色id",required = true)
                                @RequestParam String roleId){
        List<MenuRsp> menuRsps = menuService.getfunctionByRoles(roleId);
        return R.ok().message("查询成功").data("data",menuRsps);
    }

    @ApiOperation("添加菜单")
    @PostMapping("/add")
    public R addMenu(@RequestBody MenuRequest menuRequest){
        menuService.addMenu(menuRequest);
        return R.ok().message("添加成功");
    }


    @ApiOperation("编辑菜单")
    @PutMapping("update")
    public R updateMenu(@RequestBody MenuRequest menuRequest){
        menuService.updateMenu(menuRequest);
        return R.ok().message("修改成功");
    }

    @ApiOperation("根据id查询菜单")
    @GetMapping("/{menuId}")
    public R selectById(@ApiParam(value = "菜单id",required = true)
                        @PathVariable String menuId){
        Menu menu = menuService.selectById(menuId);
        return R.ok().message("查询成功").data("data",menu);
    }



    @ApiOperation("删除菜单")
    @DeleteMapping("/delete/{menuId}")
    public R deleteMenu(@ApiParam(value = "菜单id",required = true)@PathVariable String menuId,
                    @ApiParam(value = "是否确认操作",required = true)@RequestParam Boolean isConform){
        menuService.deleteMenu(menuId,isConform);
        return R.ok().message("删除成功");
    }
}
